package me.obstsalat.guildera;

import java.util.TimerTask;

import org.bukkit.Location;
import org.bukkit.entity.Player;

public class dt_compass  extends TimerTask  {
	
	private guildera plugin;
	
	dt_compass(guildera pl) {
		plugin = pl;
	}
	
	@Override
	public void run() {
		for(Player ply : plugin.getServer().getOnlinePlayers()) {
			if(ply.getWorld() == plugin.overworld) {
				if(ply.getInventory().getItemInHand().getTypeId() == 345) {
					Treasure tr = null;
					long dist = 0;
					for(Treasure tre : plugin.treasures) {
						long dx = (ply.getLocation().getBlockX()-tre.getX())*(ply.getLocation().getBlockX()-tre.getX());
						long dz = (ply.getLocation().getBlockZ()-tre.getZ())*(ply.getLocation().getBlockZ()-tre.getZ());
						if(tr == null) {
							tr = tre;
							dist = dx + dz;
						} else {
							if((dx+dz) < dist) {
								tr = tre;
								dist = dx + dz;
							}
						}
					}
					if(tr != null) {
						ply.setCompassTarget(new Location(plugin.overworld, tr.getX(), tr.getY(), tr.getZ()));
					}
				}
			}
		}
	}

}